﻿using IdentityModel;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.Security.Cryptography;

namespace JwkDemo
{
    internal class Program
    {
        /// <summary>
        /// Creates a new RSA security key.
        /// </summary>
        /// <returns></returns>
        public static void CreateRsaSecurityKey(int keySize = 2048)
        {

            var key = new RsaSecurityKey(RSA.Create(keySize))
            {
                KeyId = CryptoRandom.CreateUniqueId(16, CryptoRandom.OutputFormat.Hex)
            };

            var jwk = JsonWebKeyConverter.ConvertFromRSASecurityKey(key);
            jwk.Alg = "RS256";

            File.WriteAllText("key.jwk", JsonConvert.SerializeObject(jwk));

        }

        static void Main(string[] args)
        {
            Console.WriteLine("Create jwk ...");
            CreateRsaSecurityKey();
            Console.WriteLine("end.");
        }
    }
}
